#include <iostream>
#include <pthread.h>

using namespace std;

pthread_mutex_t cout_mutex=PTHREAD_MUTEX_INITIALIZER;
void f(){
	static int i=0;
	static pthread_mutex_t f_mutex=PTHREAD_MUTEX_INITIALIZER;
	pthread_mutex_lock(&f_mutex);
	pthread_mutex_lock(&cout_mutex);
	cout << "f:" << ++i << endl;
	pthread_mutex_unlock(&cout_mutex);
	pthread_mutex_unlock(&f_mutex);
}

int count=0;



void* f_wrapper(void*){
	count++;
	pthread_mutex_lock(&cout_mutex);
	cout <<"f_wrapper " << count << " started!"<< endl;
	pthread_mutex_unlock(&cout_mutex);
	f();
	return NULL;
}

#define NUM_THREADS 12
int main() {
	pthread_t threads[NUM_THREADS];
	for(size_t i=0; i < NUM_THREADS; i++)
			pthread_create(threads+i, NULL, &f_wrapper, NULL);

	for(size_t i=0; i < NUM_THREADS; i++)
			pthread_join(threads[i], NULL);

	return 0;

}
